package com.chenxin.wms.process.simulate.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.chenxin.common.annotation.WmsLogs;
import com.chenxin.common.core.domain.ProcResult;
import com.chenxin.common.enums.wms.TaskStatus;
import com.chenxin.common.utils.StringUtils;
import com.chenxin.wms.domain.LocationEntity;
import com.chenxin.wms.domain.StockEntity;
import com.chenxin.wms.domain.TaskEntity;
import com.chenxin.wms.domain.WcsTaskEntity;
import com.chenxin.wms.domain.dto.UpSimulateParams;
import com.chenxin.wms.mapper.LocationMapper;
import com.chenxin.wms.mapper.StockMapper;
import com.chenxin.wms.mapper.TaskMapper;
import com.chenxin.wms.mapper.WcsTaskMapper;
import com.chenxin.wms.process.simulate.IProcUpSimulate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Slf4j
@Service
public class ProcUpSimulateImpl implements IProcUpSimulate {
    @Autowired
    private WcsTaskMapper wcsTaskMapper;
    @Autowired
    private StockMapper stockMapper;
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private LocationMapper locationMapper;

    @WmsLogs(name = "模拟上架")
    @Transactional
    @Override
    public ProcResult call(UpSimulateParams params) {
        if (params == null) {
            return ProcResult.error("up_simulate_001", "请求参数不能为空");
        }
        // 获取参数
        String contNo = params.getContNo();
        if (StringUtils.isEmpty(contNo)) {
            return ProcResult.error("up_simulate_002", "上架托盘不能为空");
        }
        String locationNo = params.getLocationNo();
        if (StringUtils.isEmpty(locationNo)) {
            return ProcResult.error("up_simulate_003", "目标位置不能为空");
        }
        Long creator = params.getCreator();
        Date workDate = new Date();
        // 数据校验
        QueryWrapper<WcsTaskEntity> trkQW = new QueryWrapper<>();
        trkQW.eq("cont_no", contNo)
                .eq("trk_type", "1")
                .lt("trk_status", TaskStatus.DONE.getCode());
        WcsTaskEntity trkInfo = wcsTaskMapper.selectOne(trkQW);
        if (trkInfo == null) {
            return ProcResult.error("up_simulate_004", "托盘【" + contNo + "】未匹配到设备上架任务");
        }
        QueryWrapper<LocationEntity> locationQW = new QueryWrapper<>();
        locationQW.eq("location_no", locationNo).eq("location_vr", 1).eq("is_active", 0);
        LocationEntity locationInfo = locationMapper.selectOne(locationQW);
        if (locationInfo == null) {
            return ProcResult.error("up_simulate_005", "目标位置【" + locationNo + "】未在货位表维护或【停用】");
        }
        if (!contNo.equals(locationInfo.getContNo())) {
            return ProcResult.error("up_simulate_006", "目标位置【" + locationNo + "】已被托盘【" + locationInfo.getContNo() + "】占用");
        }
        // 处理业务任务
        QueryWrapper<TaskEntity> taskQW = new QueryWrapper<>();
        taskQW.lambda().eq(TaskEntity::getFromContNo, contNo)
                .eq(TaskEntity::getTaskStatus, TaskStatus.EXEC.getCode());
        TaskEntity taskInfo = taskMapper.selectOne(taskQW);
        if (taskInfo == null) {
            return ProcResult.error("up_simulate_007", "托盘【" + contNo + "】未匹配到【执行中】的任务数据");
        }
        String toPosition = taskInfo.getToPosition();
        // 模拟上架货位必须与任务目标货位一致
        if (!locationNo.equals(toPosition)) {
            return ProcResult.error("up_simulate_008", "上架货位【" + locationNo + "】与任务目标货位【" + toPosition + "】不一致");
        }
        // 设备任务更新（可转移历史）
        trkInfo.setTrkStatus(TaskStatus.DONE.getCode());
        trkInfo.setUpdater(creator);
        trkInfo.setUpdateDate(workDate);
        wcsTaskMapper.updateById(trkInfo);
        // 货位更新
        locationInfo.setContNo(contNo);
        locationInfo.setLocationStatus(2);
        locationInfo.setUpdater(creator);
        locationInfo.setUpdateDate(workDate);
        locationMapper.updateById(locationInfo);
        // 业务处理（处理同托盘号的所有任务）
        taskInfo.setTaskStatus(TaskStatus.DONE.getCode());
        taskInfo.setComments("模拟上架成功");
        taskInfo.setUpdater(creator);
        taskInfo.setUpdateDate(workDate);
        taskMapper.updateById(taskInfo);
        // 库存转移（）
        UpdateWrapper<StockEntity> stockUW = new UpdateWrapper<>();
        stockUW.lambda().eq(StockEntity::getContNo, contNo)
                .set(StockEntity::getLocationNo, locationNo)
                .set(StockEntity::getUpdater, creator)
                .set(StockEntity::getUpdateDate, workDate);
        stockMapper.update(stockUW);
        return ProcResult.success("模拟上架成功，托盘号【" + contNo + "】至货位【" + locationNo + "】");
    }
}
